<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 构造函数
        function myFn(name, price, [{ ylName, weight }]) {
            this.name = name
            this.price = price
            this.ingredients = [{ ylName, weight }]
        }
        // 实例化对象1
        let obj1 = new myFn('汉堡', 30, [{ ylName: '生菜', weight: 50 }])
        // 实例化对象2
        let obj2 = new myFn('炸鸡', 50, [{}])
        // 构建方法
        myFn.prototype.addIngredient = function (name, weight) {
            // 上传材料
            this.ingredients.push({ ylName: name, weight: weight })
            return this
        }
        // 删除
        myFn.prototype.removeIngredient = function (name) {
            let x = this.ingredients.findIndex(e => e.ylName === name)
            // console.log(x)
            this.ingredients.splice(x, 1)
            return this
        }
        // 计算
        myFn.prototype.calculateTotalWeight = function () {
            return this.ingredients.reduce((prev, e) => prev + e.weight, 0)
        }
        // 返回
        myFn.prototype.toString = function () {
            let res = this.ingredients.map(e => e.ylName).join('或')
            return `名称：${this.name}，价格：${this.price}，原料：${res}`
        }
        // 上传
        obj1.addIngredient('西红柿', 22)
        obj1.addIngredient('洋葱', 45)
        console.log(obj1)
        // 删除
        obj1.removeIngredient('西红柿')
        console.log(obj1)
        // 计算
        let num = obj1.calculateTotalWeight()
        console.log(num)
        // obj1的ingredients数组赋值给obj2
        obj2.ingredients=obj1.ingredients
        console.log(obj2)
        // 打印
        // **obj1
        console.log(obj1.toString())
        // **obj2
        console.log(obj2.toString())
    </script>
</body>

</html>